<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="90px" :rules="formRules">
                <el-form-item label="客户ID" prop="kehu_id">
                    <el-input v-model="formData.kehu_id" readonly  placeholder="请输入客户ID" />
                </el-form-item>
                <el-form-item label="公司名" prop="company">
                    <el-input v-model="formData.company" clearable placeholder="请输入公司名" />
                </el-form-item>
                <el-form-item label="税号" prop="taxNo">
                    <el-input v-model="formData.taxNo" clearable placeholder="请输入税号" />
                </el-form-item>
                <el-form-item label="单位地址" prop="address">
                    <el-input v-model="formData.address" clearable placeholder="请输入单位地址" />
                </el-form-item>
                <el-form-item label="电话" prop="tel">
                    <el-input v-model="formData.tel" clearable placeholder="请输入电话" />
                </el-form-item>
                <el-form-item label="开户银行" prop="bank">
                    <el-input v-model="formData.bank" clearable placeholder="请输入开户银行" />
                </el-form-item>
                <el-form-item label="银行账号" prop="bankNo">
                    <el-input v-model="formData.bankNo" clearable placeholder="请输入银行账号" />
                </el-form-item>
                <el-form-item label="开票类型" prop="type">
                    <el-select class="flex-1" v-model="formData.type" clearable placeholder="请选择开票类型">
                        <el-option 
                            v-for="(item, index) in dictData.taxtype"
                            :key="index" 
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="收票邮箱" prop="email">
                    <el-input v-model="formData.email" clearable placeholder="请输入收票邮箱" />
                </el-form-item>
                <el-form-item label="收票地址" prop="taxAddress">
                    <el-input v-model="formData.taxAddress" clearable placeholder="请输入收票地址" />
                </el-form-item>
                <el-form-item label="收票人" prop="taxName">
                    <el-input v-model="formData.taxName" clearable placeholder="请输入收票人" />
                </el-form-item>
                <el-form-item label="收票电话" prop="taxPhone">
                    <el-input v-model="formData.taxPhone" clearable placeholder="请输入收票电话" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="kehuTaxEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { apiKehuTaxAdd, apiKehuTaxEdit, apiKehuTaxDetail } from '@/api/kehu/tax'
import { useDictData } from '@/hooks/useDictOptions'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑开票信息' : '新增开票信息'
})

// 表单数据
const formData = reactive({
    id: '',
    kehu_id: '',
    company: '',
    taxNo: '',
    address: '',
    tel: '',
    bank: '',
    bankNo: '',
    type: '',
    email: '',
    taxAddress: '',
    taxName: '',
    taxPhone: '',
})


// 表单验证
const formRules = reactive<any>({

})

// 获取字典数据
const { dictData } = useDictData('taxtype')
// 获取详情
const setFormData = async (row: Record<any, any>) => {
    let data:any = {}
    if(row.open){
        data['kehu_id']=row.id
    }else{
        data = await apiKehuTaxDetail({ id: row.id })
    }
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
    
    
}


// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData,  }
    mode.value == 'edit' 
        ? await apiKehuTaxEdit(data) 
        : await apiKehuTaxAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}


defineExpose({
    open,
    setFormData
})
</script>
